package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.apache.commons.lang3.StringUtils;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.lp.LuckPermsTown;
import org.mclll.xwangplugin.util.PlayerTools;

import java.util.Collections;
import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/26 22:32
 */
public class OwnerReplaceExecutor {

    public static List<String> ownerTabComplete(CommandSender sender, String[] args) {
        switch (args.length) {
            case 3:
                return TownManage.selectList(null).stream()
                        .map(TownDO::getName)
                        .filter(townName -> townName.startsWith(args[2]))
                        .collect(java.util.stream.Collectors.toList());
            default:
                return null;
        }
    }

    public static void owner(CommandSender sender, String[] args) {
        // 城邦易主 - owner <玩家名> <城镇名> [confirm]
        if (args.length < 3) {
            sender.sendMessage(OWNER_COMMAND_FORMAT);
            return;
        }
        if (args.length == 3) {
            // 二次确认
            sender.sendMessage(
                    Component.text(String.format(OWNER_CONFIRM, args[1], args[2]))
                            .appendNewline()
                            .append(
                                    Component.text(CONFIRM_BUTTON)
                                            .clickEvent(ClickEvent.runCommand(String.format("/poitown owner %s %s confirm", args[1], args[2])))
                                            .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                            )
                            .append(Component.text("         "))
                            .append(Component.text(CANCEL_BUTTON).hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON))))
            );

            return;
        }
        if (args.length == 4 && args[3].equals("confirm")) {
            String playerName = args[1];
            String townName = args[2];
            OfflinePlayer playerInGame = PlayerTools.getOfflineOrOnlinePlayerByName(playerName);
            TownPlayerDO playerInDatabase = null;
            TownPlayerDO playerSelectParam = new TownPlayerDO();
            TownDO town = null;
            playerSelectParam.setName(playerName);
            List<TownPlayerDO> databasePlayerList = TownPlayerManage.selectList(playerSelectParam);
            if (databasePlayerList.isEmpty()) {
                sender.sendMessage(PLAYER_NOT_FOUND);
                return;
            }
            playerInDatabase = databasePlayerList.get(0);
            if (playerInDatabase.getIdentity() == PlayerTownIdentity.LEADER) {
                sender.sendMessage(PLAYER_IS_ALREADY_LEADER);
                return;
            }
            TownDO townSelectParam = new TownDO();
            townSelectParam.setName(townName);
            List<TownDO> townList = TownManage.selectList(townSelectParam);
            if (townList.isEmpty()) {
                sender.sendMessage(TOWN_NOT_FOUND);
                return;
            }
            town = townList.get(0);
            // 如果改城镇有镇长，把他变为普通玩家
            if (StringUtils.isNotBlank(town.getLeaderUuid()) && !"-".equals(town.getLeaderUuid())) {
                TownPlayerDO updateParam = new TownPlayerDO();
                updateParam.setIdentity(PlayerTownIdentity.GENSHIN);
                updateParam.setUuid(town.getLeaderUuid());
                updateParam.setTownUuid("-");
                TownPlayerManage.updateByUuid(updateParam);
                OfflinePlayer offlinePlayer = PlayerTools.getOfflineOrOnlinePlayerByUuid(town.getLeaderUuid());
                // 权限部分
                LuckPermsTown.kickPlayerFromTown(Collections.singletonList(offlinePlayer.getName()));
                // 镇长权限撤销
                LuckPermsTown.delLeader(offlinePlayer.getName());
                if (offlinePlayer.isOnline()) {
                    ((Player) offlinePlayer).sendMessage(YOU_HAVE_BEEN_DISMISSED);
                }
            }
            // 这个玩家变成镇长
            TownPlayerDO updateParam = new TownPlayerDO();
            updateParam.setUuid(playerInDatabase.getUuid());
            updateParam.setIdentity(PlayerTownIdentity.LEADER);
            updateParam.setTownUuid(town.getUuid());
            TownPlayerManage.updateByUuid(updateParam);
            // 权限部分
            LuckPermsTown.addPlayerToTown(town.getName(), Collections.singletonList(playerName));
            OfflinePlayer offlinePlayer = PlayerTools.getOfflineOrOnlinePlayerByUuid(town.getLeaderUuid());
            // 镇长权限
            LuckPermsTown.addLeader(offlinePlayer.getName());
            if (offlinePlayer.isOnline()) {
                ((Player) offlinePlayer).sendMessage(String.format(CREATE_NEW_TOWN_SUCCESS_SEND_LEADER, townName));
            }
            // 更新城镇信息
            TownDO updateTown = new TownDO();
            updateTown.setUuid(town.getUuid());
            updateTown.setLeaderUuid(playerInDatabase.getUuid());
            TownManage.updateByUuid(updateTown);
            sender.sendMessage(String.format(OWNER_SUCCESS, playerName, townName));
        }
    }
}
